package com.gzx.plugin.website.protocol.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.google.common.collect.Lists;
import com.gzx.plugin.website.protocol.entity.GzxMediationProtocol;
import com.gzx.plugin.website.protocol.mapper.GzxMediationProtocolMapper;
import com.gzx.plugin.website.protocol.service.GzxMediationProtocolSaveDbService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.consts.IntConstant;
import vip.xiaonuo.common.util.CommonCryptogramUtil;
import vip.xiaonuo.common.util.CommonDateUtil;
import vip.xiaonuo.common.util.CommonSleepUtil;

import java.util.List;
import java.util.stream.Collectors;

import static org.apache.commons.lang.StringEscapeUtils.escapeSql;

/**
 * @author zjt
 * @description
 * @date 2025/4/17 15:31
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class GzxMediationProtocolSaveDbServiceImpl implements GzxMediationProtocolSaveDbService {

    private final GzxMediationProtocolMapper mediationProtocolMapper;

    /**
     * 调解协议保存
     *
     * @param protocolList
     */
    @Override
    @DS("website")
    public void insertBatch(List<GzxMediationProtocol> protocolList) {
        String currDate = CommonDateUtil.getCurrentTimeStr();
        List<List<GzxMediationProtocol>> batchList = Lists.partition(protocolList, IntConstant.VALUE_1000);
        for (List<GzxMediationProtocol> batch : batchList) {
            long batchTime = System.currentTimeMillis();
            String sql = batch.stream()
                    .map(s -> String.format("('%s','%s','%s', '%s','%s', '%s','%s', '%s', '%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')",
                            escapeSql(s.getCompanyName()),
                            escapeSql(s.getProvAbbr()),
                            escapeSql(s.getDebtorName()),
                            escapeSql(CommonCryptogramUtil.doSm1AesEncrypt(s.getDebtorCardId())),
                            escapeSql(String.valueOf(s.getMediationNum())),
                            escapeSql(String.valueOf(s.getDocumentNumber())),
                            escapeSql(s.getPlatform()),
                            escapeSql(s.getProduct()),
                            escapeSql(String.valueOf(s.getLoanAmount())),
                            escapeSql(s.getCaseDate()),
                            escapeSql(s.getOfficialWebsite()),
                            escapeSql(s.getOfficialSealUrl()),
                            escapeSql(s.getWeChatOfficialAccount()),
                            escapeSql(s.getMediatorName()),
                            escapeSql(s.getMediationPersonPhone()),
                            escapeSql(s.getMediationCenterPhone()),
                            escapeSql(s.getSupervisionPhone()),
                            escapeSql(s.getMediationLetterDate()),
                            escapeSql(s.getContent()),
                            escapeSql(s.getExpiryDate()),
                            escapeSql(s.getConfigId().toString()),
                            escapeSql(s.getTenantId()),
                            escapeSql(s.getCreateUser()),
                            escapeSql(currDate)))
                    .collect(Collectors.joining(", "));
            mediationProtocolMapper.insertBatchByValSql(sql.replace("'null'", "null"));
            log.info("甲方还款数据导入：本批次共计：{}条，耗时：{}", batch.size(), System.currentTimeMillis() - batchTime);
            CommonSleepUtil.sleep(IntConstant.VALUE_100);
        }
    }
}
